<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\News\News;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\News\NewsClass;
use App\Models\News\NewsContent;
use App\Models\News\Tag;
use App\Helper\Build\Build;
use App\Admin\Extensions\Tools\Build as BuildButton;


class ExpertController extends Controller
{
    protected $name = '专家管理';
    use ModelForm;
    
    public function index()
    {
		// $this->replace_content();
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
	
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');
			if(request()->get('c_id', 0) >0)
			{
				$info = CollectNews::find(request()->get('c_id', 0));
				$CollectNewsContent = new CollectNewsContent($info);
				$content->body($this->formCollect($CollectNewsContent->collectNews()));
			}else{
				$content->body($this->form());
			}
            
        });
    }
	
	
	
	public function build()
	{
		$data = request()->all();
		if(empty($data['id']))return ['status'=>0, 'message'=> '请勾选要生成的稿件.'];
		
		$ids = explodeId($data['id']);
		
		$list = News::whereIn('id', $ids)->get();
		
		$Build = new Build();
		$msg = '';
		foreach($list as $row)
		{
			
			$arr = $Build->news($row);
			$msg .= $arr['msg'].chr(10);
		}
		return ['status'=>1, 'message'=> $msg];
	}
    
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        // $variable = News::whereIn('class_id', [52,53])->get();
        // foreach ($variable as $key => $value) {
        //     # code...
        //     $value->source = $value->attr['lingyu'];
        //     $value->save();
        // }dd(1);
        return Admin::grid(News::class, function (Grid $grid) {
            $grid->model()->whereIn('class_id', [52,53])->orderBy('id', 'DESC');
            $grid->tools(function ($tools) {
				$tools->append(new BuildButton());
			});
            $grid->filter(function($filter){
            
                // 去掉默认的id过滤器
                //$filter->disableIdFilter();
                
                $filter->useModal();
            
                // 在这里添加字段过滤器
                
                $filter->equal('class_id','栏目')->select(NewsClass::selectOptions());
                $filter->like('title', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                
            
            });
            
			$grid->actions(function ($actions) {
				$id = $actions->getKey();
				$url = news_url($actions->row->created_at, $id, true);//.'?r='.rand(0,1000000000);
				$actions->append('<a href="'.$url.'" target="_blank"><i class="fa fa-eye" title="预览" ></i></a>');
                $actions->disableView();
			});
    
            $grid->column('id', 'ID')->sortable();
            $grid->column('title', '标题');
            
            $grid->column('news_class.name', '栏目');            
            $grid->column('status','推荐')->options()->select(News::$status);
            
            $grid->column('created_at', '创建时间');
        });
    
	}
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(News::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('title', '姓名')->rules('required|min:1');
			
            $form->text('titleurl', '链接')->help('选填，创建后会自动生成');
            $form->image('titlepic','标题图片')->uniqueName();
            $form->number('listorder', '排序');
            $form->text('source', '商城宣讲的领域');
            $form->radio('status','审核状态')->options(News::$status)->default('1');
            $form->select('class_id', '栏目')->options(NewsClass::selectOptions())->rules('required|min:0');

            $form->embeds('attr', '其他信息', function ($form) {
                $form->radio('sex','性别')->options(['0'=>'女', '1'=>'男']);
                $form->text('gzdw', '工作单位');
                $form->text('zhiwu', '职务');
                $form->text('zhunye', '毕业院校及专业');
                $form->text('zhunti', '近两年参与的宣讲专题');
                // $form->text('lingyu', '商城宣讲的领域');
                $form->text('chengguo', '主要研究成功');
                $form->text('cailiao', '宣讲社会反响材料');
                $form->text('lytype', '擅长领域');
                $form->text('video', '视频地址');

            });
            
            $form->editor('content.content', '简介');
			$form->datetime('content.created_at', '创建时间');
            // $form->hidden('edit_user_id');
			$form->tools(function (Form\Tools $tools) {
                // 去掉`查看`checkbox
                $tools->disableView();
            });
            
			
			$form->saved(function (Form $form) {
                
				$Build = new Build();
				$news_info = News::find($form->model()->id);
				$Build->news($news_info);
				
			});
        });
    }
	
}
